<script setup lang="ts">
import { ref, reactive } from "vue";
import navbar from "@/components/navbar/navbar.vue";
import goodsWaterfallIndex from "@/components/goods/goodsWaterfallIndex.vue";
import cardBox from "./components/cardList.vue";
import cardBoxs from "./components/card.vue";
import { selltList, shopList, authenticationList } from "@/apis/market";
import { onLoad, onReachBottom } from "@dcloudio/uni-app";
import noneBox from "@/components/noneBox/noneBox.vue";
import { apiSelltList, apiShopList } from "@/apis/product";
import { useUserStore } from "@/stores/user";

const userStore = useUserStore();
const navbarFixed = ref(false);
let title = ref("榜"); //销售榜 店铺榜

const leftList: any = ref([]);
const rightLists: any = ref([]);
const isMore = ref(false);
const oldleftList: any = ref([]);
const oldrightList: any = ref([]);

const pageNum = ref(1);
const pageSize = ref(10);
const orderField = ref("default");
const orderType = ref("desc");
const types = ref(null);


const handlePageList = (data: any, total: any) => {
  for (let i = 0; i < data.length; i++) {
    // data[i].images = (data[i].images || data[i].videos).split(",");
    if (data[i]?.images) {
      data[i].images = data[i]?.images.split(",");
    }
    if (data[i]?.videos) {
      data[i].videos = data[i]?.videos.split(",");
    }
    if (i % 2 === 0) {
      // leftList.value.push(data[i]);
      oldleftList.value.push(data[i]);
    } else {
      // rightLists.value.push(data[i]);
      oldrightList.value.push(data[i]);
    }
  }
  let newarrleft = [...leftList.value, ...oldleftList.value];
  let newrightList = [...rightLists.value, ...oldrightList.value];
  const uniqueLeft = [...new Set(newarrleft.map(JSON.stringify))].map(
    JSON.parse
  );
  const uniqueRight = [...new Set(newrightList.map(JSON.stringify))].map(
    JSON.parse
  );
  leftList.value = uniqueLeft;
  rightLists.value = uniqueRight;
  isMore.value = leftList.value.length + rightLists.value.length < total;
};
const getProductPage = async (e: any = {}) => {
  let appUserId = null;
  if (userStore.userInfo === undefined) {
    appUserId = null;
  } else {
    appUserId = userStore.userInfo.id;
  }
  if (e.screen) {
    leftList.value = [];
    rightLists.value = [];
  }

  if (title.value == '珍宝档案') {
    // 珍宝档案
    let res = await apiSelltList({
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      // orderField: orderField.value,
      // orderType: orderType.value,
      // types: types.value,
      // appUserId: appUserId,
      // shopName: searchParams.value
    });
    const total = res.total;
    handlePageList(res.rows, total);
  } else {
    // 推荐榜
    let res = await apiShopList({
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      // orderField: orderField.value,
      // orderType: orderType.value,
      // types: types.value,
      // appUserId: appUserId,
      // shopName: searchParams.value
    });
    const total = res.total;
    handlePageList(res.rows, total);
  }

};

onLoad(async(options: any) => {
  console.log('options====', options);
  title.value = options.title;
  oldleftList.value = [];
  oldrightList.value = [];
  leftList.value = [];
  rightLists.value = [];
  pageNum.value = 1;
  isMore.value = false;
  pageSize.value = 10;
  await getProductPage();
});

// 触底下拉下一页
onReachBottom((e: any) => {
  if (isMore.value) {
    pageNum.value++;
    getProductPage();
  }
});
</script>

<template>
  <navbar :fixed="navbarFixed" :back="true" :title="title" />
  <view class="content">
    <goodsWaterfallIndex :leftList="leftList" :rightLists="rightLists" :title="title" :isMore="isMore" />
  </view>
  <noneBox v-if="leftList.length <= 0 && rightLists.length <= 0"></noneBox>
</template>

<style lang="scss">
.content {
  padding: 10rpx 20rpx 20rpx;
}
</style>
